package com.lindroy.androidplayer.ui.mine

import android.net.Uri
import android.os.Bundle
import androidx.fragment.app.activityViewModels
import androidx.navigation.fragment.findNavController
import androidx.paging.LoadState
import com.elvishew.xlog.XLog
import com.lindroy.androidplayer.R
import com.lindroy.androidplayer.base.App
import com.lindroy.androidplayer.base.BaseFragment
import com.lindroy.androidplayer.databinding.FragmentSettingsBinding
import com.lindroy.androidplayer.logic.model.LoginModel
import com.lindroy.androidplayer.logic.network.base.http.observeState
import com.lindroy.androidplayer.ui.dialog.ComDialog
import com.lindroy.androidplayer.ui.dialog.LoadingDialog
import com.lindroy.androidplayer.ui.webview.WebViewFragment
import com.lindroy.androidplayer.utils.*
import com.lindroy.androidplayer.viewmodels.SettingsViewModel
import com.lindroy.lib_utils.extensions.shortToast
import com.lindroy.lib_utils.statics.CacheUtil

/**
 * @author Lin
 * @date 2022/2/28
 * @function 设置页面
 */
class SettingsFragment : BaseFragment<FragmentSettingsBinding>() {

    private val viewModel by activityViewModels<SettingsViewModel>()

    override fun FragmentSettingsBinding.initView(savedInstanceState: Bundle?) {
        vm = viewModel
        initToolbar(R.string.settings)
    }

    override fun initData() {
        viewModel.setCacheSize(CacheUtil.getTotalCacheSize(requireContext()))
        viewModel.setBannerShowState(kvIsShowBanner)
        viewModel.setTopArticleShowState(kvIsShowTopArticle)
    }

    override fun FragmentSettingsBinding.initListener() {
        registerLoadingDialogListener()
        viewModel.bannerCheckedFlow.collectWhenResumed(viewLifecycleOwner) {
            kvIsShowBanner = it
        }

        viewModel.topArticleFlow.collectWhenResumed(viewLifecycleOwner) {
            kvIsShowTopArticle = it
        }

        viewModel.logoutClickLiveData.observe(viewLifecycleOwner) {
            ComDialog.create()
                .setTitle(R.string.tip_logout)
                .setOnPositiveClickListener { viewModel.logout() }
                .show(childFragmentManager)
        }

        viewModel.logoutConfirmLiveData.observeState(viewLifecycleOwner) {
            onEmpty {
                App.eventViewModel.postLoginInfo(LoginModel())
                clearUserInfo()
                viewModel.setBannerShowState(true)
                viewModel.setTopArticleShowState(true)
                findNavController().navigateUp()
            }
        }

        viewModel.privateLiveData.observe(viewLifecycleOwner) {
            WebViewFragment.start(
                this@SettingsFragment,
                url = "file:///android_asset/privacy_policy.html",
                title = Uri.decode(getString(R.string.settings_privacy_policy))
            )
        }

        viewModel.clearCacheLiveData.observe(viewLifecycleOwner) {
            CacheUtil.clearAllCache(requireContext(), object : CacheUtil.OnCacheClearListener {
                override fun onStart() {
                    LoadingDialog.show(this@SettingsFragment)
                }

                override fun onFinish(success: Boolean) {
                    LoadingDialog.dismiss(this@SettingsFragment)
                    success.yes {
                        shortToast(R.string.settings_clear_cache_success)
                    }
                    viewModel.setCacheSize(CacheUtil.getTotalCacheSize(requireContext()))
                }
            })
        }

        viewModel.aboutUsLiveData.observe(viewLifecycleOwner) {
            activityNavController.navigate(R.id.action_to_aboutUs)
        }
    }

}